﻿using NUnit.Framework;
using Sasa.QualityTools.CodeAnalysis.Common.Tests.Unit.Instruments;
using System;
using System.Collections.Generic;

namespace Sasa.QualityTools.CodeAnalysis.Common.Tests.Unit
{
    [TestFixture]
    public class LoggerTest
    {
        private List<string> buffer;


        [SetUp]
        public void SetUp()
        {
            buffer = new List<string>();
            Logger.Clear();
            Logger.Register(new CustomLogger(buffer, "a"));
            Logger.Register(new CustomLogger(buffer, "b"));
        }

        [TearDown]
        public void TearDown()
        {
            Logger.Clear();
        }

        [Test]
        [Description("Info should output information logs with registered loggers.")]
        public void TestInfo()
        {
            string[] expected = { "[i] a foo", "[i] b foo" };
            Logger.Info("foo");
            CollectionAssert.AreEqual(expected, buffer);
        }

        [Test]
        [Description("Warn should output warning logs with registered loggers.")]
        public void TestWarn()
        {
            string[] expected = { "[w] a foo", "[w] b foo" };
            Logger.Warn("foo");
            CollectionAssert.AreEqual(expected, buffer);
        }

        [Test]
        [Description("Error should output error logs with registered loggers.")]
        public void TestError()
        {
            string[] expected = { "[e] a foo", "[e] b foo" };
            Logger.Error("foo");
            CollectionAssert.AreEqual(expected, buffer);
        }

        [Test]
        [Description("Error should combine an exception message to an error log when it is passed an exception.")]
        public void TestErrorWithException()
        {
            var e = new Exception("bar");
            string[] expected = { "[ex] a foo bar", "[ex] b foo bar" };
            Logger.Error(e, "foo");
            CollectionAssert.AreEqual(expected, buffer);
        }

        [Test]
        [Description("Clear should unregister all loggers.")]
        public void TestClear()
        {
            Logger.Clear();
            Logger.Info("info");
            Logger.Warn("warn");
            Logger.Error("error");
            CollectionAssert.IsEmpty(buffer);
        }
    }
}
